<?php
namespace app\company\controller;
use think\Controller;
use think\Db;
use think\Session;
class Zw extends Controller
{
    //职位列表页面
    public function zwlist()
    {
        $company_id = Session::get('company_id');
        $res = Db::name('position p')
        ->join('joptype j','p.joptype_id=j.joptype_id')
        ->where('position_status',0)
        ->where('company_id',$company_id)
        ->select();
        //  dump($res);
        //  exit;
        $this->assign("res",$res);
        return $this->fetch();
    }
    //添加职位页面
    public function addzw()
    {
        $res = Db::name('joptype')->where('joptype_status',0)->select();
       //  dump($res);
       //  exit;
       $this->assign("res",$res);
        return $this->fetch();
    }
     //处理添加职位信息
     public function addzw1()
     { 
        $company_id = Session::get('company_id');
       $file = request()->file('position_pic');
       // dump($file);
       // exit;
       // 移动到框架应用根目录/public/uploads/ 目录下
       if($file){
           $info = $file->rule('uniqid')->validate(['size' => 5242880, 'ext' => 'jpg,jpeg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static' . DS .'uploads');
           if($info){
               //赋值图片名字
               $position_pic= $info->getFilename(); 
           }else{
               // 上传失败获取错误信息
               // echo $file->getError();
           }
       }
       $data = [
           'position_name' =>input('position_name'),
           'joptype_id' => input('joptype_id'),
           'position_site' =>input('position_site'),
           'position_money' => input('position_money'),
           'position_info' =>input('position_info'),
           'company_id' =>$company_id,
           'position_status' =>0,
           'position_time' =>date('Y-m-d H:i:s',time()),
           'position_pic' => $position_pic,
           ];
       // dump($data);
       // exit;
       $res = Db::table('position')->insert($data);
         if($res){
             return $this->success('添加成功','zw/zwlist');
         }
         else{
           return $this->error('新增失败');
         }
     }
     //删除职位
     public function deletezw($position_id)
     {
         $data = [
           'position_status'=>1,
         ];
         $res = Db::table('position')->where('position_id',$position_id)->update($data);
         if($res){
             return $this->success('删除成功','zw/zwlist');
         }
         else{
           return $this->error('删除失败');
         }
     }
      //职位详情页面
    public function zwdetails($position_id)
    {
        $res = Db::name('position p')
        ->join('joptype j','p.joptype_id=j.joptype_id')
        ->where('position_status',0)
        ->where('position_id',$position_id)
        ->select();
        //  dump($res);
        //  exit;
        $this->assign("res",$res);
        $res1 =Db::name('joptype')->where('joptype_status',0)->select();
        //  dump($res1);
        //  exit;
        $this->assign("res1",$res1);
        return $this->fetch();
    }
    //修改职位信息
    public function updatezw($position_id)
    { 
      $file = request()->file('position_pic');
      // dump($file);
      // exit;
      // 移动到框架应用根目录/public/uploads/ 目录下
      if($file == null){
          $data = [
              'position_name' =>input('position_name'),
              'joptype_id' => input('joptype_id'),
              'position_site' =>input('position_site'),
              'position_money' => input('position_money'),
              'position_info' =>input('position_info'),
            //   'company_id' =>1,
              // 'position_status' =>0,
              'position_time' =>date('Y-m-d H:i:s',time()),
              // 'position_pic' => $position_pic,
              ];
              // dump($data);
              // exit;
              $res = Db::table('position')->where('position_id',$position_id)->update($data);
        if($res){
            return $this->success('修改成功','zw/zwlist');
        }
        else{
          return $this->error('新增失败');
        }
      }else{
          if($file){
              $info = $file->rule('uniqid')->validate(['size' => 5242880, 'ext' => 'jpg,jpeg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static' . DS .'uploads');
              if($info){
                  //赋值图片名字
                  $position_pic= $info->getFilename(); 
              }else{
                  // 上传失败获取错误信息
                  // echo $file->getError();
              }
          }
          $data = [
              'position_name' =>input('position_name'),
              'joptype_id' => input('joptype_id'),
              'position_site' =>input('position_site'),
              'position_money' => input('position_money'),
              'position_info' =>input('position_info'),
            //   'company_id' =>1,
              'position_status' =>0,
              'position_time' =>date('Y-m-d H:i:s',time()),
              'position_pic' => $position_pic,
              ];
          // dump($data);
          // exit;
          $res = Db::table('position')->where('position_id',$position_id)->update($data);
            if($res){
                return $this->success('修改成功','zw/zwlist');
            }
            else{
              return $this->error('失败');
            }
      }
    }
}